// pages/address/address.js
import {ajax} from "../../../utils/httpRequest.js";
import {URI_ADDRESSLIST, URI_UPDATEDEFAULTADDRESS, URI_DELETEADDRESS} from "../../../api.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address_list: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddressList();
  },

  getAddressList() {
    ajax.get(URI_ADDRESSLIST, {}, {}, res => {
      console.log(res.data.data)
      this.setData({
        address_list: res.data.data
      });
    });
    // var page = this;
    // wx.showNavigationBarLoading();
    // wx.request({
    //   url: api.user.address_list,
    //   success: function (res) {
    //     wx.hideNavigationBarLoading();
    //     if (res.code == 0) {
    //       page.setData({
    //         address_list: res.data.list,
    //       });
    //     }
    //     page.setData({
    //       show_no_data_tip: (page.data.address_list.length == 0),
    //     });
    //   }
    // });
  },


  setDefaultAddress(e) {
    var index = e.currentTarget.dataset.index;
    var address = this.data.address_list[index];
    console.log(address.id);
    ajax.post(URI_UPDATEDEFAULTADDRESS, {
      id: address.id
    }, {}, res => {
      wx.showLoading({
        title: res.data.message,
        mask: true,
      });
      this.getAddressList();
    });
  },

  deleteAddress(e) {
    var that = this;
    var address_id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    //console.log(address_id,index);
    wx.showModal({
      title: "提示",
      content: "确认删除该收货地址？",
      showCancel: false,
      success(res) {
        if (res.confirm) {
          ajax.post(URI_DELETEADDRESS, {
            addressId: address_id
          }, {}, res => {
            wx.showToast({
              title: "删除成功！",
            });
            that.getAddressList();
          });
        }
      }
    });
  },

});